package com.wk.net.connector;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 数据对象。在服务端已建立连接的客户端
 * 
 * @author Bai Yu
 *
 */
public class AcceptedClient {
	AcceptedClient(Socket client) {
		this.client = client;
	}

	/**
	 * 建立调用流和返回流<br>
	 * 如果正常建立，则允许下列操作：<br>
	 * 1.获取已建立的调用流、返回流<br>
	 * 2.关闭连接并释放资源
	 * 
	 * @throws IOException
	 *             建立调用流、返回流失败<br>
	 *             此时相应的连接将被关闭
	 */
	public void establish() throws IOException {
		try {
			callStream = client.getInputStream();
			returnStream = client.getOutputStream();
		} catch (IOException e) {
			client.close();
			throw e;
		}
	}

	public InputStream getCallStream() {
		return callStream;
	}

	public OutputStream getReturnStream() {
		return returnStream;
	}

	public void close() {
		try {
			client.close();
		} catch (IOException e) {
		}
	}

	public String getDisplayName() {
		return client.getInetAddress().getHostAddress() + ":" + client.getPort();
	}

	private Socket client;
	InputStream callStream;
	OutputStream returnStream;
}
